- hosts: all
  become: true
  vars:
    kube_version: "v1.30"
    kube_release: "1.30.0-1.1"
  tasks:
  - name: Install packages that allow apt to be used over HTTPS
    apt:
      name: "{{ packages }}"
      state: present
    vars:
      packages:
      - apt-transport-https
      - ca-certificates
      - curl
      - gnupg-agent
      - software-properties-common

  - name: Add the apt signing key for Docker
    apt_key:
      url: https://mirrors.huaweicloud.com/docker-ce/linux/ubuntu/gpg
      state: present

  - name: Add the apt repository for the stable Docker version
    apt_repository:
      repo: deb [arch=amd64] https://mirrors.huaweicloud.com/docker-ce/linux/ubuntu jammy stable
      state: present

  - name: Install Containerd and its dependencies
    apt:
      name: "{{ packages }}"
      #state: present
      state: latest
      update_cache: yes
    vars:
      packages:
      - docker-ce
      - docker-ce-cli
      - containerd.io
    notify:
      - docker status

  - name: Copy containerd config file with owner and permissions
    ansible.builtin.copy:
      src: files/containerd-config.toml
      dest: /etc/containerd/config.toml
      owner: root
      group: root
      mode: '0640'
    register: containerdconfig

  - name: Restart containerd.io
    service:
      name: containerd
      daemon_reload: yes
      state: restarted
    when: containerdconfig.changed

  - name: Copy crictl config file with owner and permissions
    ansible.builtin.copy:
      src: files/crictl.yaml
      dest: /etc/crictl.yaml
      owner: root
      group: root
      mode: '0644'

  - name: Remove swapfile from /etc/fstab
    mount:
      name: "{{ item }}"
      fstype: swap
      state: absent
    with_items:
      - swap
      - none

  - name: Disable swap
    command: swapoff -a
    when: ansible_swaptotal_mb > 0

  - name: Add the apt signing key for Kubernetes
    ansible.builtin.apt_key:
      url: https://mirrors.aliyun.com/kubernetes-new/core/stable/{{ kube_version }}/deb/Release.key
      keyring: /etc/apt/keyrings/kubernetes-apt-keyring.gpg
      state: present

  - name: Add the apt repository for Kubernetes
    apt_repository:
      repo: deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://mirrors.aliyun.com/kubernetes-new/core/stable/{{ kube_version }}/deb/ /
      state: present
      filename: kubernetes.list

  - name: Install Kubernetes binaries
    apt:
      name: "{{ packages }}"
      update_cache: yes
    vars:
      packages:
        - kubelet={{ kube_release }}
        - kubeadm={{ kube_release }}
        - kubectl={{ kube_release }}

  - name: Configure --node-ip for kubelet
    lineinfile:
      path: /etc/default/kubelet
      regexp: "^KUBELET_EXTRA_ARGS=.*--node-ip="
      line: KUBELET_EXTRA_ARGS=--node-ip={{ node_ip }}
      create: yes
    register: kubeletconfig

  - name: Restart kubelet
    service:
      name: kubelet
      daemon_reload: yes
      state: restarted
    when: kubeletconfig.changed

  handlers:
    - name: docker status
      service: name=docker state=started